﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Practices.ServiceLocation;
using Microsoft.Practices.Prism.Events;
using Microsoft.Practices.Prism.Regions;
using Tanmia.Basis.ServiceInterface;

namespace SaryaAccounting.ModuleMenu
{
    /// <summary>
    /// Interaction logic for ModuleNavigatorControl.xaml
    /// </summary>
    public partial class ModuleNavigatorControl : UserControl
    {
        IRegionManager regionManager = null;

        public ModuleNavigatorControl()
        {
            InitializeComponent();

            try
            {
                regionManager = ServiceLocator.Current.GetInstance<IRegionManager>();
                IEventAggregator eventAggregator = ServiceLocator.Current.GetInstance<IEventAggregator>();

                if (eventAggregator != null)
                    eventAggregator.GetEvent<NavigationItemAddedEvent>().Subscribe(AddNavigationItem);
            }
            catch { }
        }

        private void AddNavigationItem(NavigationItem navItem)
        {
            if (navItem == null)
                throw new ArgumentNullException("navItem");

            TreeViewItem parent = GetTreeViewItem(tvNavigator.Items, navItem.Parent);
            TreeViewItem child = NavigationTreeViewItem.ToTreeViewItem(navItem);
            child.MouseDoubleClick += new MouseButtonEventHandler(child_MouseDoubleClick);

            if (parent == null)
            {
                child.Style = FindResource("ModuleHeaderStyle") as Style;
                tvNavigator.Items.Add(child);
            }
            else
            {
                child.Style = FindResource("ModuleItemStyle") as Style;
                parent.Items.Add(child);
            }
        }

        private void child_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            TreeViewItem tvItem = sender as TreeViewItem;
            if (tvItem == null)
                return;

            NavigationItem navItem = tvItem.Tag as NavigationItem;
            if (navItem == null)
                return;

            if (string.IsNullOrEmpty(navItem.RegionName) || string.IsNullOrEmpty(navItem.ViewName))
                return;

            regionManager.RequestNavigate(navItem.RegionName, navItem.ViewName);
        }

        private TreeViewItem GetTreeViewItem(ItemCollection itemCollection, NavigationItem navItem)
        {
            foreach (TreeViewItem item in itemCollection)
            {
                if (item.Tag == navItem)
                    return item;
                else
                    if (item.Items.Count > 0)
                    {
                        TreeViewItem tvItem = GetTreeViewItem(item.Items, navItem);
                        if (tvItem != null)
                            return tvItem;
                    }

            }
            return null;
        }
    }

    public class NavigationTreeViewItem : TreeViewItem
    {
        public static TreeViewItem ToTreeViewItem(NavigationItem navItem)
        {
            NavigationTreeViewItem tvItem = new NavigationTreeViewItem();
            tvItem.Header = navItem.Title;
            tvItem.ToolTip = navItem.Description;
            tvItem.Tag = navItem;
            return tvItem;
        }
    }
}
